/*
* aliyun 短信服务
* */
const router = require("express-promise-router")();
const AccessKeyId = "LTAI4HXjTo7IkzVM";
const AccessKeySecret = "Hp8dQd19TOqcKrhb0h7PoiZb0Ue9vU";
const SignName = "交作业";
const ReportBackTemplateCode = "SMS_245185247";
const Core = require('@alicloud/pop-core');
const client = new Core({
    accessKeyId: AccessKeyId,
    accessKeySecret: AccessKeySecret,
    endpoint: 'https://dysmsapi.aliyuncs.com',
    apiVersion: '2017-05-25'
});
const requestOption = {
    method: 'POST'
};
//给学生发报告退回的短信
function sendReportBackMsg(phoneNumber,reportId,boxName,score_item,backReason){
    if(!phoneNumber||phoneNumber.trim().length==0){
        //throw new Error(`短信通知失败：没有学生手机号码`);
        return {status:false,error:`发送退回报告通知短信失败：手机号码为空`, type:'student', reportId:reportId};
    }
    const params = {
        "PhoneNumbers": phoneNumber,
        "SignName": SignName,
        "TemplateCode": ReportBackTemplateCode,
        "TemplateParam": `{\"boxName\":\"${boxName}\",\"score_item\":\"${score_item}\",\"backReason\":\"${backReason}\"}`
    };
    return client.request('SendSms', params, requestOption).then( (result) => {
        //console.log("sendMsg success",result);
        return {status:true}

    }, (ex) => {
        //console.log("sendMsg fail",ex);
        return {status:false,error:"发送退回报告通知短信失败："+ex.data.Message, type:'student', phone:phoneNumber, reportId:reportId};
    });
}

module.exports={
    sendReportBackMsg
};